package com.newasia.server.common.checkHandler;

import com.newasia.server.common.exception.ValidateException;
import com.newasia.server.common.result.ManagementRetCode;
import com.newasia.server.controller.form.UserForm;
import jodd.util.StringUtil;

/**
 * @author LiChong
 * @description 用户名规范检测
 * @date 2020/4/2
 */

public class UsernameNormCheckHandler extends CheckHandler {

    @Override
    public void doHandler(UserForm form) {
        String username = form.getUsername();
        if(StringUtil.isEmpty(username)){
            throw new ValidateException(
                    ManagementRetCode.USERORPASSWORD_NOT_EXIST.getCode(),
                    ManagementRetCode.USERORPASSWORD_NOT_EXIST.getMessage());
        }
        //用户名是否不符合规范
        String rex = "[^0-9]{1}[\u4e00-\u9fa5a-zA-Z0-9]*";
        if (!username.matches(rex)) {
            throw new ValidateException(
                    ManagementRetCode.ILLEGAL_USERNAME.getCode(),
                    ManagementRetCode.ILLEGAL_USERNAME.getMessage());
        }
        if(null != next) {
            next.doHandler(form);
        }
    }
}
